package com.cloudwise.pollux.common.response;

import com.cloudwise.pollux.common.exception.CommonException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一 Rest 返回对象
 */
@Data
@Accessors(chain = true)
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 5426736816445955890L;
    private String code;
    private String message;
    private T data;

    public static <T> Response<T> success() {
        return new Response<T>()
                .setCode(Status.SUCCEED.getCode())
                .setMessage(Status.SUCCEED.getMessage());
    }

    public static <T> Response<T> noData() {
        return new Response<T>()
                .setCode(Status.NO_DATA.getCode())
                .setMessage(Status.NO_DATA.getMessage());
    }

    public static <T> Response<T> process() {
        return new Response<T>()
                .setCode(Status.PROCESS.getCode())
                .setMessage(Status.PROCESS.getMessage());
    }

    public static <T> Response<T> success(T data) {
        Response<T> success = success();
        success.setData(data);
        return success;
    }

    public static <T> Response<T> failure(String errorMessage) {
        return new Response<T>()
                .setCode(Status.FAILED.getCode())
                .setMessage(errorMessage);
    }

    public static <T> Response<T> failure(String errorMessage, T data) {
        return new Response<T>()
                .setCode(Status.FAILED.getCode())
                .setMessage(errorMessage)
                .setData(data);
    }

    public static <T> Response<T> exception(CommonException e) {
        return new Response<T>()
                .setCode(e.getCode())
                .setMessage(e.getMessage());
    }

    public boolean isSuccess() {
        return Status.SUCCEED.getCode().equals(this.code);
    }

    public boolean isProcess() {
        return Status.PROCESS.getCode().equals(this.code);
    }

    public boolean isFailure() {
        return Status.FAILED.getCode().equals(this.code);
    }



    @Getter
    @AllArgsConstructor
    public enum Status {
        /**
         * 状态
         */
        SUCCEED("0000", "处理成功"),
        NO_DATA("0001", "查询成功,无数据"),
        PROCESS("4000", "处理中"),
        FAILED("3000", "处理失败"),
        LOGIN_OUT("2001", "登录信息已失效，请重新登录"),
        EXCEPTION("9999", "出现异常");

        private String code;
        private String message;
    }
}
